<?php
include "common.php";
class Manage extends Common {
    const IMGPATH = 'uploads/';

	function __construct() {
		parent::__construct ();
		$this->load->database ();
	}

    /**
     * 用户中心
     */
    function userMember()
    {
        $uid = $this->session->userdata['id'];

        $data['checkUser'] = $this->Muser->checkUserDataIsPerFect($uid);
        $data['madouKa'] = $this->Muser->checkUserMadouKa($uid);
        $data['checkAlbum'] = $this->Malbum->checkCreateAlbum($uid);
        $data['checkPhone'] = $this->Muser->checkPhone($uid);
        $data['checkWeibo'] = $this->Mweibo->checkWeibo($uid);
        $data['user'] = $this->Muser->getById ( $uid );
        $data['icon'] = $this->UserIconStatus();
        $data['memberMethod'] = 'member';

        $this->load->view ( 'userMember', $data );
    }

    function message()
    {
        $uid = $this->session->userdata ['id'];
        $type = isset($_GET['type']) ? $_GET['type'] :2;
        $data['message'] = $this->Msms->getUserSms($uid,$type,15);

        $data['memberMethod'] = 'message';
        $data['type'] = $type;
        $this->load->view ( 'message', $data );
    }

    function setSmsRead()
    {
        $id = isset($_GET['id']) ? $_GET['id'] :'';

        $this->Msms->setSmsRead($id);
    }

	function marchive() {
		$user = $this->getUserArray ();
		$data ['user'] = $user;
		//$this->load->view ( 'managearchive' );
		//$this->load->view ( 'headzs' );
		$data ['user'] = $user;
		//$this->load->view ( 'headht', $data );
		$ht = false;
		$data2 ['ht'] = $ht;
        $data2['memberMethod'] = 'madouinfo';
        $data2['title'] = '模特信息';
		$this->load->view ( 'archivepart', $data2 );
	}
	function getUserArray() {
		$id = $this->session->userdata ['id'];
		$arr = $this->Muser->getById ( $id );
		return $arr;
	}
	function mcard() {
		$user = $this->getUserArray ();
		$data ['user'] = $user;
		$ht = true;
		$data ['ht'] = $ht;
        $data['memberMethod'] = 'madouk';
        $data['title'] = '模特卡';
		$this->load->view ( 'card', $data );
	}
	function userinfo() {
		$user = $this->getUserArray ();
		$data ['user'] = $user;
		$ht = true;
		$data ['ht'] = $ht;
		$province = $this->Mprovince->getAll ();
		$data ["province"] = $province;
        $data['title'] = '基本信息';
        $data['memberMethod'] = 'info';
		$this->load->view ( 'archive', $data );
	}
	function experience() {
		$user = $this->getUserArray ();
		$data ['user'] = $user;
		$ht = true;
		$data ['ht'] = $ht;
        $data['memberMethod'] = 'activity';
        $data['title'] = '活动经历';
		$this->load->view ( 'experience', $data );
	}
	function malbum() {
		$uid = $this->session->userdata['id'];
		$this->Malbum->initAlbum($uid);
		$user = $this->getUserArray ();
		$data ['user'] = $user;
		$ht = true;
		$data ['ht'] = $ht;
        $data['title'] = '相册管理';
        $data['memberMethod'] = 'album';
		$this->load->view ( 'album', $data );
	}
	
	function checkExistsName(){
		$arr = array();
		$name = $this->input->post('name');
		$uid = $this->session->userdata['id'];
		$exists = $this->Malbum->checkExistsName($uid,$name);
		if($exists){
			$arr["ReturnCode"] = 0;
			$arr["Msg"] = "已存在该专辑名";
		}else{
			$arr["ReturnCode"] = 1;
		}
		echo json_encode($arr);
	}
	function saveAlbumName() {
		$arr = array ();
		$name = $this->input->post ( 'name' );
		$uid = $this->session->userdata ['id'];
		$query = $this->Malbum->saveAlbumName ( $uid, $name );
		$arr ["ReturnCode"] = 1;
		echo json_encode ( $arr );
	}
	function loadAlbums() {
		$arr = array ();
		$uid = $this->session->userdata ['id'];
		$query = $this->Malbum->loadAlbums ( $uid );
		$result = "";
		$names = "";
		foreach ( $query as $row ) {
			$result .= "<li><img src='images/vip_img.jpg' />
			<div class='xc_sl'>共";
			$result .= $this->Malbumdetail->getCount ( $row->id );
			$result .= "张</div> <span>" . $row->name . "</span></li>";
			
			$names .= "<option value='".$row->id."'>".$row->name."</option>";
		}
		$arr ["album"] = $result;
		$arr["albumNames"] = $names;
		echo json_encode($arr);
	}
	
	function loadAlbumsLink() {

        $albumId = (int)$this->input->post('album_id');

        if(!empty($albumId)){
            $uid = $albumId;
            $url = 'index.php/login/albumDetail?id='.$albumId.'&albumid=';
        }else{
            $uid = $this->session->userdata ['id'];
            $url = 'index.php/login/loadAlbumDetail?albumid=';
        }

        if(empty($uid)){
            echo json_encode(array('statusCode'=>'2','msg'=>'参数错误'));
            exit;
        }

		$query = $this->Malbum->loadAlbums ( $uid );

		$result = "";
		$names = "";
        $arr = array ();
		foreach ( $query as $row ) {
			$result .= "<li><a href='".$url.$row->id."'><img src='".base_url().self::IMGPATH.$row->url."' /></a>
			<div class='xc_sl'>共";
			$result .= $this->Malbumdetail->getCount ( $row->id );
			$result .= "张</div> <span>" . $row->name . "</span></li>";
				
			$names .= "<option value='".$row->id."'>".$row->name."</option>";
		}
		$arr ["album"] = $result;
		$arr["albumNames"] = $names;
		echo json_encode($arr);
	}
	
	function saveAlbumDetail(){
		$arr = array();
		$albumid = $this->input->post("albumid");
		$url = $this->input->post('url');
        $uid = $this->session->userdata['id'];
        $is_img = explode('.',$url);
		!empty($is_img[1]) && $this->Malbumdetail->saveAlbumDetail($albumid,$url,$uid);
		$arr["ReturnCode"] = 1;
		echo json_encode($arr);
	}
	
	function midcard(){
		$user = $this->getUserArray ();
		$data ['user'] = $user;
		$ht = true;
		$data ['ht'] = $ht;
        $data['title'] = '认证管理';
        $data['memberMethod'] = 'xxx';
		$this->load->view ( 'idcard', $data );
	}
	function mweibo(){
		$user = $this->getUserArray ();
		$data ['user'] = $user;
		$ht = true;
		$data ['ht'] = $ht;
        $data['title'] = '账号绑定';
        $data['memberMethod'] = 'bind';
		$this->load->view ( 'weibo', $data );
	}
	function mpwd(){
		$user = $this->getUserArray ();
		$data ['user'] = $user;
		$ht = true;
		$data ['ht'] = $ht;
        $data['title'] = '密码修改';
        $data['memberMethod'] = 'password';
		$this->load->view ( 'modifypwd', $data );
	}
	function memailphone(){
		$user = $this->getUserArray ();
		$data ['user'] = $user;
		$ht = true;
		$data ['ht'] = $ht;
        $data['memberMethod'] = 'phonebind';
		$this->load->view ( 'emailphone', $data );
	}
	
	function checkPwd(){
		$arr = array();
		$arr["ReturnCode"] = 0;
		$pwd = $this->input->post('pwd');
		$id = $this->session->userdata['id'];
		$exists = $this->Muser->checkPwd($id,$pwd);
		if($exists){
			$arr["ReturnCode"] = 1;
		}else{
			$arr["Msg"] ="密码不正确";
		}
		echo json_encode($arr);
		
	}
	function modifyPwd(){
		$arr = array();
		$arr["ReturnCode"] = 0;
		$pwd = $this->input->post('pwd');
		$pwdnew = $this->input->post('pwdnew');
		$id = $this->session->userdata['id'];
		$num = $this->Muser->modifyPwd($id,$pwd,$pwdnew);
		if($num>0)
			$arr["ReturnCode"] = 1;
		echo json_encode($arr);
	}
	
	function saveIdCard(){
		$arr = array();
		$arr["ReturnCode"] = 0;
		$url =$this->input->post('idcardurl');
		$id = $this->session->userdata['id'];
		$this->Muser->updateIdCard($id,$url);
		$arr["ReturnCode"] = 1;
		echo json_encode($arr);
	}
	function saveIdCard2(){
		$arr = array();
		$arr["ReturnCode"] = 0;
		$url =$this->input->post('idcardurl');
		$id = $this->session->userdata['id'];
		$this->Muser->updateIdCard2($id,$url);
		$arr["ReturnCode"] = 1;
		echo json_encode($arr);
	}
	function saveIdCardNo(){
		$arr = array();
		$arr["ReturnCode"] = 0;
		$idcardno =$this->input->post('idcardno');
		$id = $this->session->userdata['id'];
		$this->Muser->updateIdCardNo($id,$idcardno);
		$arr["ReturnCode"] = 1;
		echo json_encode($arr);
	}
	
	function saveWeibo(){
		$arr = array();
		$arr["ReturnCode"] = 0;
		$wid = $this->input->post('wid');
		$url = $this->input->post('url');
		$uid = $this->session->userdata['id'];
		$this->Mweibo->saveWeibo($uid,$wid,$url);
		$arr["ReturnCode"] = 1;
		echo json_encode($arr);
	}
	function delWeibo(){
		$arr = array(
			"ReturnCode" => 0
		);
		$wid = $this->input->post('wid');
		$uid = $this->session->userdata['id'];
		$this->Mweibo->delWeibo($wid,$uid);
		$arr["ReturnCode"] = 1;
		echo json_encode($arr);
	}
	function loadWeibo(){
		$uid = $this->session->userdata['id'];
		$query = $this->Mweibo->loadWeibo($uid);
		$result = "";
		foreach ($query as $row){
			$result.=$row->wid."/";
		}
		$result=trim($result,"/");
		$result="/$result/";
		$arr = array();
		$arr["wid"] = $result;
		echo json_encode($arr);		
	}
	
	function test() {
		$this->load->view ( 'test' );
	}

    /**
     * 设置头像
     */
    function settingPhoto()
    {
        $uid = $this->session->userdata['id'];
        $data['user'] = $this->Muser->getUserPhoto($uid);
        $data['memberMethod'] = 'photo';
        $this->load->view ( 'settingPhoto' ,$data);
    }

    /**
     * 更新头像
     */
    function savePhoto()
    {
        $uid = $this->session->userdata['id'];
        $headurl = $this->input->post('phtoUrl');
        $return = $this->Muser->updateHeadUrl ( $uid, $headurl );

        if($return){
            $this->setUserCodeImg($uid);
            $data['msg'] = '上传成功';
            $data['statusCode'] = 1;
        }else{
            $data['msg'] = '上传失败';
            $data['statusCode'] = 2;
        }
        echo json_encode($data);
    }

    function UserIconStatus()
    {
        $uid = $this->session->userdata ['id'];
        $icon = $this->Muserimg->getUserIcon($uid);

        $html = '';
        $baseUrl = base_url();
        $mourl = 'images/icon_star.png';
        if(!empty($icon)){

            for($i=0;$i<5;$i++){
                $url = isset($icon[$i]['url']) ? $icon[$i]['url'] : $mourl;
                $name = isset($icon[$i]['name']) ? $icon[$i]['name'] : '';
                $html .= '<a href="javascript:void(0)"><img src="'.$baseUrl.$url.'" title="'.$name.'"/> </a>';
            }
        }else{
            $html = '<a href="javascript:void(0)"><img src="'.$baseUrl.$mourl.'" /> </a>
            <a href="javascript:void(0)"><img src="'.$baseUrl.$mourl.'" /> </a>
            <a href="javascript:void(0)"><img src="'.$baseUrl.$mourl.'" /> </a>
            <a href="javascript:void(0)"><img src="'.$baseUrl.$mourl.'" /> </a>
            <a href="javascript:void(0)"><img src="'.$baseUrl.$mourl.'" /> </a>';
        }

        return $html;
    }

    /**
     * @param $uid
     */
    function setUserCodeImg($uid)
    {
        $usre = $this->Muser->getUserByUid ($uid);
        $dir = dirname(__FILE__);
        $imgUrl = $dir.'/../../uploads/'.$usre['headurl'];

        if(is_file($imgUrl)){
            $codeImg = $imgUrl;
        }else{
            $codeImg = $dir.'/../../images/touxiang_img.jpg';
        }

        $url = base_url().'index.php/login/showperson?id='.$usre['id'];
        $imgCode = $dir.'/../../imgCode/'.$usre['codeimg'];
        $xiangyangFile = dirname($imgCode).'/'.$uid.'_xiangyang.png';
        if(is_file($imgCode) || is_file($xiangyangFile)){
            @unlink($imgCode);
            @unlink($xiangyangFile);
        }

        $userCodeImg = $this->getUserCode($url,$codeImg,$usre['id']);
        $this->Muser->setUserCode($uid,$userCodeImg);
    }

    function getUserCode($url,$imgUrl,$uid=0)
    {
        //生成中间带logo的二维码
        //$this->load->library('phpqrcode/phpqrcode');
        $dir = dirname(__FILE__);
        include($dir.'/../../system/libraries/phpqrcode/phpqrcode.php');
        $dataTime = date('Ymd',time());
        $codeDir = $dir.'/../../imgCode/'.$dataTime;
        if(!file_exists($codeDir)){
            @mkdir($codeDir);
        }

        $QR = $codeDir.'/'.$uid.'_xiangyang.png';
        $value=$url;
        $userCodeImg = $codeDir.'/'.$uid.'.png';
        $errorCorrectionLevel = 'L';
        $matrixPointSize = 10;
        QRcode::png($value, $QR, $errorCorrectionLevel, $matrixPointSize, 2);

        $logo = $imgUrl;

        if($logo !== FALSE)
        {

            $QR = imagecreatefromstring(file_get_contents($QR));
            $logo = imagecreatefromstring(file_get_contents($logo));
            $QR_width = imagesx($QR);
            $QR_height = imagesy($QR);
            $logo_width = imagesx($logo);
            $logo_height = imagesy($logo);
            $logo_qr_width = $QR_width / 5;
            $scale = $logo_width / $logo_qr_width;
            $logo_qr_height = $logo_height / $scale;
            $from_width = ($QR_width - $logo_qr_width) / 2;
            imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
        }
        $return = imagepng($QR,$userCodeImg);
        if($return){
            return $dataTime.'/'.$uid.'.png';
        }else{
            return '';
        }
    }
}
?>